<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
	Integer uid = token.getUid();
	Integer patientUid = input.getInt("patient_uid", -1);
	List<UserProfile> userInfo = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("uid=", patientUid).and("self=", SELF)).query().getResultObj();
	if (isEmpty(userInfo)) {
		 Tools.encryptPrint(out, new Message(-1, "没有您要查询的这个患者信息"));
		 return;
	}
	UserProfile userProfile = userInfo.get(0);
	List<Map> plist = QBuilder.db("basedb").select().from("patient_info").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("patient_uid=", patientUid)).query().getResultObj();
	userProfile.setAge(input.getInt("age", userProfile.getAge()));
	userProfile.setGender(input.getInt("gender", userProfile.getGender()));
	if (!isEmpty(plist)) {
		Map pMap = plist.get(0);
		String inHospitalNumber = input.getDesc("in_hospital_number", Tools.getDesc(pMap.get("in_hospital_number"), ""));
		String inDate = input.getDesc("in_date", Tools.getDesc(pMap.get("in_date"), ""));
		String doctorComment = input.getDesc("doctor_comment", Tools.getDesc(pMap.get("doctor_comment"), ""));
		String inHospitalDesc = input.getDesc("in_hospital_desc", Tools.getDesc(pMap.get("in_hospital_desc"), ""));
		String outHospitalDesc = input.getDesc("out_hospital_desc", Tools.getDesc(pMap.get("out_hospital_desc"), ""));
		String bedNumber = input.getDesc("bed_number", Tools.getDesc(pMap.get("bed_number"), ""));
		String outDate = input.getDesc("out_date", Tools.getDesc(pMap.get("out_date"), ""));
		pMap.put("in_hospital_number", inHospitalNumber);
		pMap.put("in_date", inDate);
		pMap.put("out_date", outDate);
		pMap.put("doctor_comment", doctorComment);
		pMap.put("in_hospital_desc", inHospitalDesc);
		pMap.put("out_hospital_desc", outHospitalDesc);
		pMap.put("bed_number", bedNumber);
		pMap.put("mod_user", uid + "");
		pMap.put("mod_time", System.currentTimeMillis());
		pMap.put("hospital", hospital);
		pMap.put("status", STATUS_ENABLE);
		pMap.put("json", "{}");
		DBTools.db("basedb").modItem("patient_info", pMap);
	} else {
		Map pMap = new HashMap();
		pMap.put("patient_uid", patientUid);
		String inHospitalNumber = input.getDesc("in_hospital_number", "");
		String inDate = input.getDesc("in_date",  "");
		String doctorComment = input.getDesc("doctor_comment",  "");
		String inHospitalDesc = input.getDesc("in_hospital_desc",  "");
		String outHospitalDesc = input.getDesc("out_hospital_desc", "");
		pMap.put("in_hospital_number", inHospitalNumber);
		pMap.put("in_date", inDate);
		pMap.put("doctor_comment", doctorComment);
		pMap.put("in_hospital_desc", inHospitalDesc);
		pMap.put("out_hospital_desc", outHospitalDesc);
		pMap.put("create_user", uid + "");
		pMap.put("create_time", System.currentTimeMillis());
		pMap.put("hospital", hospital);
		pMap.put("status", STATUS_ENABLE);
		pMap.put("json", "{}");
		DBTools.db("basedb").addItem("patient_info", pMap);
	}
	DBTools.db("basedb").modItem(userProfile, userProfile.getId());
    Tools.encryptPrint(out, msg);
%>
